/*
 * @Author: sunkaiyuan sunkaiyuan@namenode.com
 * @Date: 2023-01-13 13:11:48
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-07-12 10:46:21
 * @Description: 
 */
const { defineConfig } = require('@vue/cli-service')
const CompressionPlugin = require("compression-webpack-plugin");
const fs = require('fs')
const path = require('path')
module.exports = defineConfig({
	//设置为空打包后不分更目录还是多级目录
	publicPath: '', 
	//build编译后存放静态文件的目录
	//assetsDir: "static",

	// build编译后不生成资源MAP文件
	productionSourceMap: false,

	//开发服务,build后的生产模式还需nginx代理
	devServer: {
		allowedHosts: 'all',
		open: true,
		// server: {
		// 	type: 'https',
		// 	options: {
		// 		cert: fs.readFileSync(path.join(__dirname, '../../ssl/server.crt')),
		// 		key: fs.readFileSync(path.join(__dirname, '../../ssl/server.key'))
		// 	},
		// },
		open: false, //运行后自动打开浏览器
		port: process.env.VUE_APP_PORT, //挂载端口
		proxy: {
			'/api': {
				target: process.env.VUE_APP_API_BASEURL,
				ws: true,
				pathRewrite: {
					'^/api': ''
				}
			}
		}
	},

	chainWebpack: config => {
		// 移除 prefetch 插件
		config.plugins.delete('preload');
		config.plugins.delete('prefetch');
		config.resolve.alias.set('vue-i18n', 'vue-i18n/dist/vue-i18n.cjs.js');
	},

	configureWebpack: {
		//性能提示
		performance: {
			hints: false
		},
		optimization: {
			splitChunks: {
				chunks: "all",
				automaticNameDelimiter: '~',
				name: "scuiChunks",
				cacheGroups: {
					//第三方库抽离
					vendor: {
						name: "modules",
						test: /[\\/]node_modules[\\/]/,
						priority: -10
					},
					elicons: {
						name: "elicons",
						test: /[\\/]node_modules[\\/]@element-plus[\\/]icons-vue[\\/]/
					},
					tinymce: {
						name: "tinymce",
						test: /[\\/]node_modules[\\/]tinymce[\\/]/
					},
					echarts: {
						name: "echarts",
						test: /[\\/]node_modules[\\/]echarts[\\/]/
					},
					xgplayer: {
						name: "xgplayer",
						test: /[\\/]node_modules[\\/]xgplayer.*[\\/]/
					},
					codemirror: {
						name: "codemirror",
						test: /[\\/]node_modules[\\/]codemirror[\\/]/
					},
					vxetable: {
						name: "vxetable",
						test: /[\\/]node_modules[\\/]vxe-table[\\/]/
					}
				}
			}
		},
		plugins: [
			new CompressionPlugin({
				algorithm: 'gzip', // 使用gzip压缩
				test: /\.js$|\.html$|\.css$/, // 匹配文件名
				filename: '[path][base].gz[query]', // 压缩后的文件名(保持原文件名，后缀加.gz)
				minRatio: 1, // 压缩率小于1才会压缩
				threshold: 10240, // 对超过10k的数据压缩
				deleteOriginalAssets: false, // 是否删除未压缩的源文件，谨慎设置，如果希望提供非gzip的资源，可不设置或者设置为false（比如删除打包后的gz后还可以加载到原始资源文件）
			}),
		]
	}
})